package com.server.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.model.all.enums.SystemEnum;
import com.server.model.all.enums.YesNoEnum;
import com.server.model.all.system.bo.AddBlackWhiteListBo;
import com.server.model.all.system.bo.RemoveBlackWhiteListBo;
import com.server.model.all.system.entity.BlackWhiteListEntity;
import com.server.system.mapper.BlackWhiteListMapper;
import com.server.system.service.BlackWhiteListService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlackWhiteListEntityServiceImpl extends ServiceImpl<BlackWhiteListMapper, BlackWhiteListEntity> implements BlackWhiteListService {


    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 添加名单
     * @author LXC
     * @date 2023/2/1 20:29
     */
    @Override
    public boolean addBlackWhiteList(List<AddBlackWhiteListBo> addBlackWhiteListBoList) {
        List<BlackWhiteListEntity> blackWhiteListEntityList = new ArrayList<>();

        for (AddBlackWhiteListBo addBlackWhiteListBo:addBlackWhiteListBoList){
            BlackWhiteListEntity blackWhiteListEntity = BeanUtil.copyProperties(addBlackWhiteListBo, BlackWhiteListEntity.class);
            blackWhiteListEntity.setStatus(addBlackWhiteListBo.getStateEnum().getCode());

            //判断是否已经存在
            long count = count(new LambdaQueryWrapper<BlackWhiteListEntity>()
                    .eq(BlackWhiteListEntity::getIp, addBlackWhiteListBo.getIp())
                    .eq(BlackWhiteListEntity::getStatus, addBlackWhiteListBo.getStateEnum().getCode())
                    .eq(BlackWhiteListEntity::getDelFlag, YesNoEnum.NO.getCode())
            );
            if(count > 0){
                continue;
            }
            //添加redis
            redisTemplate.opsForSet().add(addBlackWhiteListBo.getStateEnum().getRedisKey(), addBlackWhiteListBo.getIp());

            blackWhiteListEntityList.add(blackWhiteListEntity);
        }

        return saveBatch(blackWhiteListEntityList);
    }

    /**
     * 移除名单
     * @author LXC
     * @date 2023/2/1 20:29
     */
    @Override
    public boolean removeBlackWhiteList(List<RemoveBlackWhiteListBo> removeBlackWhiteListBoList) {

        for (RemoveBlackWhiteListBo removeBlackWhiteListBo:removeBlackWhiteListBoList){
            BlackWhiteListEntity blackWhiteListEntity = BeanUtil.copyProperties(removeBlackWhiteListBo, BlackWhiteListEntity.class);
            blackWhiteListEntity.setStatus(removeBlackWhiteListBo.getStateEnum().getCode());
            blackWhiteListEntity.setDelFlag(YesNoEnum.YES.getCode());

            //更新
            update(blackWhiteListEntity,new LambdaQueryWrapper<BlackWhiteListEntity>()
                    .eq(BlackWhiteListEntity::getBlackWhiteListId,removeBlackWhiteListBo.getBlackWhiteListId())
                    .eq(BlackWhiteListEntity::getStatus,removeBlackWhiteListBo.getStateEnum().getCode())
                    .eq(BlackWhiteListEntity::getDelFlag,YesNoEnum.NO.getCode())
            );

            //添加redis
            redisTemplate.opsForSet().remove(removeBlackWhiteListBo.getStateEnum().getRedisKey(), blackWhiteListEntity.getIp());

        }

        return true;
    }

    @Override
    public List<String> getBlackWhiteIpAddr(SystemEnum.blackWhiteListStateEnum stateEnum) {
        return list(new LambdaQueryWrapper<BlackWhiteListEntity>()
                .select(BlackWhiteListEntity::getIp)
                .eq(BlackWhiteListEntity::getStatus,stateEnum.getCode())
                .eq(BlackWhiteListEntity::getDelFlag,YesNoEnum.NO.getCode())
        ).stream().map(BlackWhiteListEntity::getIp).collect(Collectors.toList());
    }


}
